package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* DataStoreView
* */
class DataStoreView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_FF8A80)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.FILL_AND_STROKE
            color = context.resources.getColor(R.color.color_FF8A80)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy + cy / 8)

            val mStrokeWidth = (cx / 10).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth

            mPath.apply {
                reset()
                addArc(
                    -cx * 9 / 10,
                    0F,
                    cx * 9 / 10,
                    cy / 2,
                    -30F,
                    230F
                )
            }
            drawPath(mPath, mPaint0)

            mPath.apply {
                reset()
                addArc(
                    -cx * 9 / 10,
                    0F - cy * 1 / 4,
                    cx * 9 / 10,
                    cy / 2 - cy * 1 / 4,
                    -30F,
                    230F
                )
            }
            drawPath(mPath, mPaint0)

            mPath.apply {
                reset()
                addArc(
                    -cx * 9 / 10,
                    0F - cy * 2 / 4,
                    cx * 9 / 10,
                    cy / 2 - cy * 2 / 4,
                    -30F,
                    230F
                )
            }
            drawPath(mPath, mPaint0)

            mPath.apply {
                reset()
                addArc(
                    -cx * 9 / 10,
                    0F - cy * 3 / 4,
                    cx * 9 / 10,
                    cy / 2 - cy * 3 / 4,
                    0F,
                    360F
                )
            }
            mPaint1.strokeWidth = mStrokeWidth
            drawPath(mPath, mPaint1)

            restore()
        }
    }
}